﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Profile;
using System.Text;
using Gift.DAL;
using Gift.Model;


namespace WebSite.cn
{
    public partial class products_detfrm4 : BasePage
    {
        // Fields

        readonly DALComment theComment = new DALComment();
        readonly DALMember theMember = new DALMember();
        tbMember oMember;
        public string member = "游客";
        public string members = "游客";
        public string pinmsg = "";
        string commentForm = " <div class=\"comment\"> "
                              + " <div class=\"comment_user\" >注册会员：{0}</div> "
                              + " <div class=\"con_right\" > "
                               + "   <div class=\"con_level\">"
                               + "       <div class=\"fleft\" > 评分：{1}</div> "
                                + "      <div  class=\"fright\" >{2}</div>"
                                + "  </div> "
                                + "  <div class=\"con_body\">"
                                + "    <div class='con_title'><span>标题：</span>{3}</div> <div class='con_txt'><span>评分内容：</span>{4}</div> "
                                + "  </div>"
                               + " </div>"
                           + " </div>";


        public string GetComment()
        {
            StringBuilder sbBody = new StringBuilder();
            StringBuilder sbStar = new StringBuilder();
            if(!string.IsNullOrEmpty(base.Request.QueryString["gid"]))
            {
              DataTable table = theComment.GetList("isnull(memberId,0) != 0 and proid=" + re.replace(base.Request.QueryString["gid"])).Tables[0] ;
              if (table != null)
              {
                  foreach (DataRow dr in table.Rows)
                  {

                          oMember = theMember.GetModel(int.Parse(dr["memberId"].ToString()));
                      if(oMember != null)
                      {
                          sbStar.Remove(0, sbStar.Length);
                                for (int i = 1; i <= Convert.ToInt32(dr["score"]); i++)
                                {
                                   sbStar.Append( "<img src='images/star.gif' />");
                                }
                                for (int j = 0; j < (5 - Convert.ToInt32(dr["score"])); j++)
                                {
                                      sbStar.Append( "<img src='images/star1.gif' />");
                                }

                                sbBody.AppendFormat(commentForm
                                    , string.IsNullOrEmpty(oMember.membername) ? oMember.memberlogin : oMember.membername
                                    , sbStar
                                    , dr["updatetime"].ToString()
                                    , dr["title"].ToString()
                                    , dr["content"].ToString());

                      }

                  }
              }
            }
            return sbBody.ToString();
        }

        // Methods
        private void Bind()
        {
            if (base.LoginUserMember != null && !string.IsNullOrEmpty(base.LoginUserMember.MemberId))
            {
                this.members = base.LoginUserMember.MemberName;
            }
            else
            {
                this.Button1.Visible = false;
                this.btnReset.Visible = false;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string str;

            if (base.LoginUserMember != null && !string.IsNullOrEmpty(base.LoginUserMember.MemberId))
            {
                str = base.LoginUserMember.MemberId;
            }
            else
            {
                str = "0";
            }


            SQLAccess.DoSql(string.Concat(new object[] { "insert into tbComment (memberid,types,proid,ipadd,updatetime,content,title,score) values (", str, ",1,", re.replace(base.Request.QueryString["gid"]), ",'", HttpContext.Current.Request.UserHostAddress, "','", DateTime.Now, "','", re.replace(base.Request.Form["content"]), "','", re.replace(base.Request.Form["title"]), "',", base.Request.Form["score"], ")" }));
            if (info.info1("", "shopisfig").ToString() == "1")
            {
                base.Response.Write("<script language='javascript'>alert('评论提交成功,要经过管理员审核才能显示！');window.location.href='products_detfrm4.aspx?gid=" + re.replace(base.Request.QueryString["gid"]) + "';</script>");
                base.Response.End();
            }
            else
            {
                base.Response.Write("<script language='javascript'>alert('评论提交成功！');window.location.href='products_detfrm4.aspx?gid=" + re.replace(base.Request.QueryString["gid"]) + "';</script>");
                base.Response.End();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack && (base.Request.QueryString["gid"] != null))
            {
                this.Bind();
            }
        }


    }
}